package com.wealth.system.web.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import com.wealth.system.entity.User;
import com.wealth.system.service.ShiroUser;

import iaas.upload.ApiUploadService;
import iaas.utils.ResultObject;
import net.sf.json.JSONObject;


@Controller
@RequestMapping
public class BaseController {
	Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private ApiUploadService apiUploadService;	

	@ModelAttribute
	public void init(Model model,HttpServletRequest request){
		if(SecurityUtils.getSubject().isAuthenticated()){
			model.addAttribute("shiroUser", ShiroUser.getInstance());
		}
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		//注册自定义的属性编辑器，适合日期
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor dateEditor = new CustomDateEditor(df, true);
		// 表示如果命令对象有Date类型的属性，将使用该属性编辑器进行类型转换
		binder.registerCustomEditor(Date.class, dateEditor);
	}
	
	public User getCurrentUser(){
		return ShiroUser.getInstance().getUser();
	}
	
	
	@RequestMapping(value="/manage/index",method = RequestMethod.GET)
	public String index(){
		try{
			return "index";
		}catch(Exception e){
			logger.error("控制台出错", e);
			return "/system/500";
		}
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
		return "login";
	}
	
	@RequestMapping(value="/logout",method = RequestMethod.GET)
	public String logout(){
		SecurityUtils.getSubject().logout();
		return "redirect:/login";
	} 
	
	
	/**
	 * 上传图片
 	 * --------功能/参数说明----------
	 * 该方法会压缩图片
	 */
	@SuppressWarnings(value="all")
	@RequestMapping(value="/richtext/upload/pic/{width}/{height}",method =RequestMethod.POST)
	@ResponseBody
	public JSONObject uploadPicRichtext(DefaultMultipartHttpServletRequest request,
			@PathVariable Integer width,@PathVariable Integer height){
		try {
			MultipartFile file = request.getFile("imgFile");
			String filepath = apiUploadService.uploadPictureFile(file, "", width, height, false, false, request);
			String json = "{\"error\": 0,\"url\": \""+filepath+"\"}";
			return new JSONObject().fromObject(json);
//			response.setCharacterEncoding("UTF-8");
//			PrintWriter out = response.getWriter();
//			out.write(json);
//			out.flush();
//			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 上传图片
 	 * --------功能/参数说明----------
	 * 该方法不会压缩图片
	 * width = 0
	 * height = 0
	 */
	@RequestMapping(value="/upload/pic",method =RequestMethod.POST)
	@ResponseBody
	public ResultObject uplaodProductPic(DefaultMultipartHttpServletRequest request){
		MultipartFile file = request.getFile("file");
		String filepath = apiUploadService.uploadPictureFile(file, "", 0, 0, false, false, request);
		return new ResultObject("200","",filepath);
		
	}
	
//	/**
//	 * 上传图片
//	 * --------功能/参数说明----------
//	 * 该方法会压缩图片
//	 * width = {width}
//	 * height ={height}
//	 */
//	@RequestMapping(value="/upload/pic/{width}/{height}",method =RequestMethod.POST)
//	@ResponseBody
//	public ResultObject uplaodPicWidthAndHeight(DefaultMultipartHttpServletRequest request,@PathVariable Integer width,@PathVariable Integer height){
//		MultipartFile file = request.getFile("file");
//		String filepath = apiUploadService.uploadPictureFileToTwo(file, "","", width, height, true, false, request);
//		return new ResultObject("200","",filepath);
//	}
	/**
	 * 上传图片
	 * --------功能/参数说明----------
	 * 该方法会压缩图片
	 * width = {width}
	 * height ={height}
	 */
	@RequestMapping(value="/upload/pic/{width}/{height}",method =RequestMethod.POST)
	@ResponseBody
	public ResultObject uplaodPicWidthAndHeight(DefaultMultipartHttpServletRequest request,@PathVariable Integer width,@PathVariable Integer height){
		MultipartFile file = request.getFile("file");
		String filepath = apiUploadService.uploadPictureFile(file, "", width, height, false, false, request);
		return new ResultObject("200","",filepath);
	}

	/**
	 * 上传图片:同时生成原图、缩略图和水印图
 	 * --------功能/参数说明----------
	 * 该方法不会压缩图片
	 * width = 0
	 * height = 0
	 */
	@RequestMapping(value="/upload/product/pic",method =RequestMethod.POST)
	@ResponseBody
	public ResultObject uplaodProductPicToThree(DefaultMultipartHttpServletRequest request){
		MultipartFile file = request.getFile("file");
		
		String waterlogo = BaseController.class.getResource("/images/water_logo.png").toString();
		int index = waterlogo.indexOf("/");
		String waterlogosuffix = waterlogo.substring(index+1);
		
		String filepath = apiUploadService.uploadPictureFileToThree(file,waterlogosuffix, "", 0, 0, false, false, request);
		return new ResultObject("200","",filepath);
		
	}
	
}
